package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import com.jt.vo.EasyUITree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ItemMapper itemMapper;
    @Override
    public ItemCat findItemCatById(Long itemCatId) {
        return itemCatMapper.selectById(itemCatId);
    }

    /**
     * 根据parentId 查询子级目录信息
     * 获取ItemCatList集合，之后件List集合转化为VoList集合
     * @param parentId
     * @return
     */
    @Override
    public List<EasyUITree> findItemCatList(Long parentId) {
        //准备返回数据（list集合）这是一个vo封装数据类型
        ArrayList<EasyUITree> treeList = new ArrayList<>();
        //实现数据库的查询
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        List<ItemCat> catList = itemCatMapper.selectList(queryWrapper);
        //实现数据的转化，cat List转化为tree List
        for (ItemCat itemCat:catList) {
            //获取id
            long id = itemCat.getId();
            //获取商品名称
            String text = itemCat.getName();
            //判断拿取到的数据是否为父级，如果不是父级则应该open如果是应该close
            String stat = itemCat.getIsParent()?"closed":"open";
            EasyUITree easyUITree = new EasyUITree(id, text, stat);
            treeList.add(easyUITree);
        }
        return treeList;
    }


}
